<?php
//包含数据库配置文件
include '../../public/dbconfig.php';
//包含函数库文件
include '../../public/functions.php';
//接收id
if($_POST['sid']){
	$sidarr = $_POST['sid'];
	$sid = implode(',',$sidarr);
}else{
	$sid = $_GET['sid'];
}

//调用函数 链接数据库
$link = con();

//删除之前查询该用户是否发帖 没发帖就可以删除 发了帖子就把帖子和回帖都删除
$usql = "select id,uid,cid from ftiezi where uid in({$sid})";
//执行sql
$ures = mysqli_query($link, $usql);
//判断执行结果
if($ures && mysqli_num_rows($ures)){
	//查询该用户发的帖子
	$fsql = "select f.id,f.uid,f.cid from ftiezi f,user u where f.uid=u.id and f.uid in({$sid})";
	//执行sql
	$fres = mysqli_query($link, $fsql);
	//判断执行结果
	if($fres && mysqli_num_rows($fres)){
		while($finfo = mysqli_fetch_assoc($fres)){
			//判断主贴下 是否有回帖
			$hsql = "select htiezi.id,fid,ftiezi.title from htiezi,ftiezi where htiezi.fid=ftiezi.id and ftiezi.id in({$finfo['id']})";
			//执行sql
			$hres = mysqli_query($link,$hsql);
			//判断执行结果
			if($hres && mysqli_num_rows($hres)){
				while($hinfo = mysqli_fetch_assoc($hres)){
					//主贴下面有回帖
					//准备语句删除回帖
					$sql2 = "delete from htiezi where id in({$hinfo['id']})";
					//执行sql
					$res2 = mysqli_query($link, $sql2);
				}
			}
			//准备删除语句
			$sql3 = "delete from ftiezi where id in({$finfo['id']})";
			//执行sql
			$res3 = mysqli_query($link,$sql3);
		}
	//释放结果集
	mysqli_free_result($fres);
	}
//释放结果集
mysqli_free_result($ures);
}
//准备SQL
$sql = "delete from user where id in({$sid})";

//执行SQL
$res = mysqli_query($link, $sql);

//判断执行结果
if($res){
	echo '<script>alert("删除成功");window.location.href="list.php";</script>';
}else{
	echo '<script>alert("删除失败");window.location.href="list.php";</script>';
}

//关闭数据库链接
mysqli_close($link);